﻿layui.config({
    base: "Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 10, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };

    //表格数据和分页
    function getList(data) {
        $.ajax({
            type: "POST",
            url: "/Check/Check",
            data: data,
            dataType: "json",
            success: function (data) {
                var num = JSON.parse(data.Checks)
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;
                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //进行判断
                if (num >= 2) {
                    $("#signin").attr("style", "display:none;");
                }
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert("123");
            }
        });
        //渲染数据
        function renderDate(list) {
            var dataHtml = '';

            if (list != null) {
                layui.each(list, function (index, item) {
                    if (item.AttendanceType == true) {
                        if (item.Violate == true) {
                            item.Violate = "迟到"
                        } else {
                            item.Violate = "无"
                        }

                    } else {
                        if (item.Violate == true) {
                            item.Violate = "早退"
                        } else {
                            item.Violate = "无"
                        }
                    }
                    dataHtml += '<tr>'
                    + '<td>' + item.AttendId + '</td>'
                    + '<td align="left">' + item.Name + '</td>'
                    + '<td>' + item.AddTime + '</td>'
                     + '<td>' + (item.AttendanceType == true ? "签到" : "签退") + '</td>'
                      + '<td>' + item.Violate + '</td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="8">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            $('.news_list thead input[type="checkbox"]').prop("checked", false);
            form.render(); //渲染表单
        }
    }
    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.RowCount,//得到总页数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 5, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }

    $(function () {
        getList(pageData);
        $(document).on("click", "#signin", function () {
            $.ajax({
                type: "POST",
                url: "/Check/signin",//发送请求  
                success: function (result) {
                    getList(pageData);
                    layer.alert(result.Message, { icon: 1, time: 1.5 * 1000 }, function (index) {
                           
                        });
                }
            });
            return false;
        });
        form.on('submit(Select)', function (data) {
            var where = data.field;
            pageData.where = JSON.stringify(where);
            getList(pageData);
        })
    });
})
